#ifndef QUTRUNNER_H
#define QUTRUNNER_H
#include "QuickLib.h"

#include <QObject>

#include <QHash>
#include <QList>
class QLibrary;

#include <core/Singleton.h>

#include "QutCase.h"
#include "QutFunction.h"
#include "QutSummary.h"

class QUICKSHARED_EXPORT QutRunner : public QObject, public Singleton<QutRunner>
{
    Q_OBJECT

private:
    typedef quint64 FunctionKey;
    typedef void (*VoidVoidFunction)(void);
    enum State
    {
        Null=0,
        Waiting,
        Active,
        Complete,
        Error,
        Size
    };

public:
    explicit QutRunner(QObject * parent=0);
    QString Test(QLibrary * lib);
    bool enter(QutFunction * fnp,
               const QString & fni,
               const QString & file,
               const int line);
    void add(QutFunction * fnp,
             QutCase event);
    void leave(QutFunction * fnp);
    void finish(void);
    int eventCount(void) const;
    QutCase eventAt(const int index) const;
    QutCase::Level maxLevel(void) const;
    QutCase::List eventList(void) const;
    QutSummary summary(void) const;

signals:
    void addTestCase(QutCase);

private:
    //State state(FunctionInfo * fni) const;
    void setState(const FunctionKey key,
                  const State state);
    void takeAction(void); // based upon max_sev
    FunctionKey grindFunctionKey(void);
    FunctionKey key(void) const;
    bool isError(const QutCase::Level level) const;
    QString message(const QutCase::Level level,
                    const QString & message);

//public:
  //  static QutRunner * instance(void);

private:
    QutSummary _summary;
    QutCase::Level max_level;
    QutCase::Level error_level;
    QutCase::List event_list;
    QHash<QutFunction *, FunctionKey> fnp_key_hash;
    QMap<FunctionKey, QutFunction *> key_fnp_map;
    QMap<FunctionKey, QString> key_fni_map;
    QMap<FunctionKey, QString> key_file_map;
    QMap<FunctionKey, int> key_line_map;
    QMap<FunctionKey, State> key_state_map;
    int mLibrary_k;
};

//typedef Singleton<QutRunner> QutRun;

#endif // QUTRUNNER_H
